package com.spzx.user.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.common.core.utils.StringUtils;
import com.spzx.user.domain.Region;
import com.spzx.user.domain.UserAddress;
import com.spzx.user.mapper.RegionMapper;
import com.spzx.user.mapper.UserAddressMapper;
import com.spzx.user.service.RegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class RegionServiceImpl extends ServiceImpl<UserAddressMapper, UserAddress> implements RegionService {
    @Autowired
    RegionMapper regionMapper;

    @Override
    public String getNameByCode(String code) {
        if (StringUtils.isEmpty(code)) {
            return "";
        }
        QueryWrapper<Region> regionQueryWrapper = new QueryWrapper<>();
        regionQueryWrapper.eq("code", code);
        Region region = regionMapper.selectOne(regionQueryWrapper);
        String name = region.getName();
        if(null != region) {
            return name;
        }
        return "";
    }

    @Override
    public String getCodeByName(String name) {
        if (StringUtils.isEmpty(name)) {
            return "";
        }
        QueryWrapper<Region> regionQueryWrapper = new QueryWrapper<>();
        regionQueryWrapper.eq("name", name);
        Region region = regionMapper.selectOne(regionQueryWrapper);
        String code = region.getCode();
        if(null != region) {
            return code;
        }
        return "";
    }

    @Override
    public List<Region> getAddressList(String parentCode) {
        if (StringUtils.isEmpty(parentCode)) {
            return null;
        }
        QueryWrapper<Region> regionQueryWrapper = new QueryWrapper<>();
        regionQueryWrapper.eq("parent_code", parentCode);
        List<Region> regions = regionMapper.selectList(regionQueryWrapper);
        if (regions.isEmpty()) {
            return new ArrayList<>();
        }
        return regions;
    }


}
